package com.zTest;

import lombok.Data;

public class Practice {

	public static void main(String[] args) {
		HeroNode heroNode1 = new HeroNode(1, "zhangsan", "zs");
		HeroNode heroNode2 = new HeroNode(2, "lisi", "ls");
		HeroNode heroNode3 = new HeroNode(3, "wangwu", "ww");
		HeroNode heroNode4 = new HeroNode(4, "zhaoliu", "zl");

		SingleLinkedList singleLinkedList = new SingleLinkedList();
		singleLinkedList.addByOrder(heroNode1);
		singleLinkedList.addByOrder(heroNode4);
		singleLinkedList.addByOrder(heroNode2);
		singleLinkedList.addByOrder(heroNode3);
		singleLinkedList.list();

		HeroNode lastIndexNode = singleLinkedList.findLastIndexNode(singleLinkedList.getHead(), 2);
		System.out.println(lastIndexNode);

		System.out.println("========= reverse =========");
		singleLinkedList.reverseList(singleLinkedList.getHead());
		singleLinkedList.list();

		System.out.println("=======update========");
		HeroNode heroNode4New = new HeroNode(4, "zhaoliu111", "zl111");
		singleLinkedList.update(heroNode4New);
		singleLinkedList.list();

		System.out.println("=======del===========");
		singleLinkedList.del(2);
		singleLinkedList.list();
		int length = singleLinkedList.getLength(singleLinkedList.getHead());
		System.out.println("The Length is：" + length);

	}
}

class SingleLinkedList {
	private HeroNode head = new HeroNode(0, "", "");

	public HeroNode getHead() {
		return this.head;
	}

	public void add(HeroNode node) {
		HeroNode temp = head;
		while (temp.next != null) {
			temp = temp.next;
		}
		temp.next = node;
	}

	public void list() {
		HeroNode temp = head.next;
		if (temp == null) {
			System.out.println("list is empty");
		}
		while (temp != null) {
			System.out.println(temp);
			temp = temp.next;
		}
	}

	public void addByOrder(HeroNode node) {
		HeroNode temp = head;
		while (true) {
			if (temp.next == null) {
				break;
			}
			if (temp.next.no > node.no) {
				break;
			}
			if (temp.next.no == node.no) {
				System.out.println("no is exit");
				return;
			}
			temp = temp.next;
		}
		node.next = temp.next;
		temp.next = node;
	}

	public void update(HeroNode node) {
		if (head.next == null) {
			System.out.println("list is empty");
			return;
		}
		HeroNode temp = head;
		while (temp != null) {
			if (temp.no == node.no) {
				temp.name = node.name;
				temp.nickName = node.nickName;
				return;
			}
			temp = temp.next;
		}
		System.out.println("no is not exit");
	}

	public void del(int no) {
		if (head.next == null) {
			System.out.println("list is empty");
			return;
		}
		HeroNode temp = head;
		while (temp.next != null) {
			if (temp.next.no == no) {
				temp.next = temp.next.next;
			}
			temp = temp.next;
		}
	}

	public int getLength(HeroNode head) {
		int length = 0;
		if (head.next == null) {
			return length;
		}
		HeroNode temp = head;
		while (temp.next != null) {
			length++;
			temp = temp.next;
		}
		return length;
	}

	public HeroNode findLastIndexNode(HeroNode head, int index) {
		if (head.next == null) {
			return null;
		}
		int length = getLength(head);
		if (index < 0 || index > length) {
			return null;
		}
		HeroNode temp = head.next;
		for (int i = 0; i < length - index; i++) {
			temp = temp.next;
		}
		return temp;
	}

	public void reverseList(HeroNode head) {
		if (head.next == null || head.next.next == null) {
			return;
		}
		HeroNode temp = head.next;
		HeroNode next;
		HeroNode newHeader = new HeroNode(0, "", "");
		while (temp != null) {
			next = temp.next;

			temp.next = newHeader.next;
			newHeader.next = temp;

			temp = next;
		}
		head.next = newHeader.next;
	}
}


@Data
class HeroNode {
	public int no;
	public String name;
	public String nickName;
	public HeroNode next;

	public HeroNode(int no, String name, String nickName) {
		this.no = no;
		this.name = name;
		this.nickName = nickName;
	}

	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer("HeroNode{");
		sb.append("no=").append(no);
		sb.append(", name='").append(name).append('\'');
		sb.append(", nickName='").append(nickName).append('\'');
		sb.append('}');
		return sb.toString();
	}
}